package com.example.crm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.crm.entity.Contact;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 联系人Mapper接口
 */
@Mapper
public interface ContactMapper extends BaseMapper<Contact> {
    
    /**
     * 根据客户ID查询联系人列表
     */
    @Select("SELECT c.*, cu.name as customer_name FROM crm_contact c " +
            "LEFT JOIN crm_customer cu ON c.customer_id = cu.id " +
            "WHERE c.customer_id = #{customerId} AND c.deleted = 0")
    List<Contact> selectByCustomerId(@Param("customerId") Long customerId);
    
    /**
     * 获取客户的主要联系人
     */
    @Select("SELECT c.*, cu.name as customer_name FROM crm_contact c " +
            "LEFT JOIN crm_customer cu ON c.customer_id = cu.id " +
            "WHERE c.customer_id = #{customerId} AND c.is_primary = 1 AND c.deleted = 0 LIMIT 1")
    Contact selectPrimaryContactByCustomerId(@Param("customerId") Long customerId);
} 